// JavaScript Document
function admin_configure()
{
	// 평점 갱신
	$( '#btn_caculateModelPoints' ).click(function(e) {
		$.post(
			getGatePath()
			,
			{
				__key__: 'calculateModelAVG',
				__returnType__: 'json',
				login_member_id: login_member_id(),
				login_member_session: login_member_session()
			}, 
			function( data )
			{
				if( data.result == "SUCCESS" )
				{
					setResult( '[정치인 점수 평균 계산] 성공' );
				}else{
					setResultFailForJSON( '[정치인 점수 평균 계산] 실패', data );
				}
			}
			,
			'json' );
    });
	
	// 서버 점검 등록
	$( '#serverNoticeForm' ).submit(function(e) {
        e.preventDefault();
		
		var _formData = new FormData( document.getElementById( 'serverNoticeForm' ) );
		_formData.append( '__key__', 'insertServerNotice' );
		_formData.append( '__returnType__', 'json' );
		_formData.append( 'login_member_id', login_member_id() );
		_formData.append( 'login_member_session', login_member_session() );
		
		$.ajax(
		{
			url: getGatePath(),
			data: _formData,
			dataType:'json',
			processData: false,
			contentType: false,
			type: "POST",
			success: function( data )
			{
				if( data.result == 'SUCCESS' )
				{
					setResult( '[공지 추가] 성공' );
				}else{
					setResultFailForJSON( '[공지 추가] 실패', data );
				}
			}
		});
    });
	// 목록 조회
	var _noticelistPage 	= new PageInfo( document.getElementById( 'serverNoticeList' ) );
	$( "#btn_refreshServerNotice" ).click(function(e) {
		$.post( getGatePath(),
		{
			__key__: 'selectAllNotices',
			__returnType__: 'json',
			login_member_id: login_member_id(),
			login_member_session: login_member_session(),
			page: _noticelistPage.current,
			numPerPage: _noticelistPage.numPerPage
		},
		function( data )
		{
			if( data.result == "SUCCESS" )
			{
				// 완료 콜백
				if( noticeRefreshSuccess )
				{
					noticeRefreshSuccess.call( null, data );
				}
			}else{
				// 실패 콜백
				if( noticeRefreshFail )
				{
					noticeRefreshFail.call( null, data );
				}
			}
		},
		'json');
	});
	
	// 조회 성공
	function noticeRefreshSuccess( data )
	{
		// 성공 핸들러
		setResult( '[공지 조회] 성공' );
		
		var $listContainer = $( "#serverNoticeList>.list" );
		// 비우기
		$listContainer.find( '.btnEdit' ).unbind( "click" );
		$listContainer.find( '.btnDelete' ).unbind( "click" );
		$listContainer.empty();
		
		// 타이틀 추가
		var _tr;
		_tr = "<thead>";
		_tr += "<tr>";
		_tr += 		"<td>NO</td>";
		_tr +=  	"<td>서버 접속 제한</td>";
		_tr +=  	"<td>시작</td>";
		_tr +=  	"<td>종료</td>";
		_tr +=  	"<td>메세지</td>";
		_tr +=  	"<td>등록자</td>";
		_tr +=  	"<td>등록일</td>";
		_tr +=  	"<td>활성여부</td>";
		_tr +=  	"<td>EDIT</td>";
		_tr +=  	"<td>DELETE</td>";
		_tr += "</tr>";
		_tr += "</thead>";
		_tr += "<tbody>";
		// 리스트 추가
		var _no = (_noticelistPage.current-1)*_noticelistPage.numPerPage+1;
		for( var index in data.notice )
		{
			var _item = data.notice[ index ];
			_tr += "<tr>";
			_tr += 		"<td>"+(_no++)+"</td>";
			_tr += 		"<td>"+toStringByBoolean( _item[ 'server_lock' ] )+"</td>";
			_tr += 		"<td>"+_item[ 'start' ]+"</td>";
			_tr += 		"<td>"+_item[ 'end' ]+"</td>";
			_tr += 		"<td>"+_item[ 'message' ]+"</td>";
			_tr += 		"<td>"+_item[ 'owner_nickname' ]+"</td>";
			_tr += 		"<td>"+_item[ 'regist_date' ]+"</td>";
			_tr += 		"<td>"+toStringByBoolean( _item[ 'enable' ] )+"</td>";
			_tr += 		"<td>"+"<input type='button' class='btnEdit' value='EDIT' data='"+_item[ 'seq' ]+"'/>"+"</td>";
			_tr += 		"<td>"+"<input type='button' class='btnDelete' value='DELETE' data='"+_item[ 'seq' ]+"'/>"+"</td>";
			_tr += "</tr>";
		}
		_tr += "</tbody>";
		$listContainer.append( _tr );
		
		// 수정 버튼 이벤트 등록
		$listContainer.find( '.btnEdit' ).bind( "click", null, function(e) {
			// alert( 'EDIT '+$(this).attr( 'data' ) );
			var $notice_seq = $(this).attr( 'data' );
			showUpdateNoticePopup( $notice_seq );
		});
		// 삭제 버튼 이벤트 등록
		$listContainer.find( '.btnDelete' ).bind( "click", null, function(e) {
			alert( '미구현 :: DELETE '+$(this).attr( 'data' ) );
		});
		
		// 페이징
		_noticelistPage.setTotalCount( data.count );
		_noticelistPage.updateLabelField();
	}
	
	// 조회 실패
	function noticeRefreshFail( data )
	{
		setResultFailForJSON( '[공지 조회] 실패', data );
	};
	
	// 상세 팝업 출현
	function showUpdateNoticePopup( $notice_seq )
	{
		$.post( getGatePath(),
		{
			__key__: 'selectNotice',
			__returnType__: 'json',
			login_member_id: login_member_id(),
			login_member_session: login_member_session(),
			seq: $notice_seq
		}, 
		function( data )
		{
			if( data.result == "SUCCESS" )
			{
				setResult( '[공지 단일 정보 조회] 성공' );
				
				// [[ 팝업 셋팅 ]]
				$popup = $( "#updateServerNotice" );
				$popup.show();
				
				// 팝업이 설정과 동시에 공통변수 설정
				$popup.find( '[name="seq"]' ).val( 				data[ 'seq' ] );
				$popup.find( '[name="serverLock"]' ).val( 		data[ 'server_lock' ] );
				$popup.find( '[name="start"]' ).val( 			convertDatetimelocalValue( data[ 'start' ] ) );
				$popup.find( '[name="end"]' ).val( 				convertDatetimelocalValue( data[ 'end' ] ) );
				$popup.find( '[name="message"]' ).val( 			data[ 'message' ] );
				$popup.find( '[name="ownerNickname"]' ).val( 	data[ 'owner_nickname' ] );
				$popup.find( '[name="ownerId"]' ).val( 		data[ 'owner_id' ] );
				$popup.find( '[name="registDate"]' ).val( 		convertDatetimelocalValue( data[ 'regist_date' ] ) );
				$popup.find( '[name="enable"]' ).val( 			data[ 'enable' ] );
				
				
			}else{
				setResultFailForJSON( '[공지 단일 정보 조회] 실패', data );
			}
		},
		'json' );
	}
	// 상세 팝업 내용 저장
	$( '#updateServerNoticeForm' ).submit(function(e) {
        e.preventDefault();
		
		var _formData = new FormData( document.getElementById( 'updateServerNoticeForm' ) );
		_formData.append( '__key__', 'updateServerNotice' );
		_formData.append( '__returnType__', 'json' );
		_formData.append( 'login_member_id', login_member_id() );
		_formData.append( 'login_member_session', login_member_session() );
		
		$.ajax(
		{
			url: getGatePath(),
			data: _formData,
			dataType:'json',
			processData: false,
			contentType: false,
			type: "POST",
			success: function( data )
			{
				if( data.result == 'SUCCESS' )
				{
					setResult( '[공지 변경] 성공' );
					// 갱신
					$( "#btn_refreshServerNotice" ).click();
				}else{
					setResultFailForJSON( '[공지 변경] 실패', data );
				}
			}
		});
    });
};